///////////////////////////////////////////////////////////////////////////////
//
//
//
///////////////////////////////////////////////////////////////////////////////

package model.service
{
    import com.adobe.serialization.json.JSON;

    import flash.events.IOErrorEvent;
    import flash.net.URLRequest;
    import flash.net.URLRequestMethod;
    import flash.net.URLVariables;

    import model.vo.LogRecord;

    import org.kolonitsky.alexey.debug.DebugWrapper;
    import org.kolonitsky.alexey.net.ElfinSerivce;

    /**
     * Complete event dispatched when operation is complete.
     */
    [Event(name="complete", type="flash.events.Event")]


    /**
     * Server communication for manipulate record log. This class directly used by
     * LogList view component.
     */
    public class LogService extends ElfinSerivce
    {
        //---------------------------------------------------------------------
        //  Properties
        //---------------------------------------------------------------------

        /**
         * This method pars loaded data, because don't call it offen.
         * @return Array of LogRecords object.
         */
        public function get log():Array
        {
            var result:Array = new Array;
            var rawArray:Array = null;
            try
            {
                rawArray = lastData as Array;
            }
            catch (error:Error)
            {
                DebugWrapper.trace("ERROR: LogService.log: Loaded log is not Array");
            }

            var n:int = rawArray.length;
            DebugWrapper.trace("DEBUG: LogService.log: recordCount = " + n);
            for (var i:int = 0; i < n; i++)
                result.push(new LogRecord(rawArray[i]));

            return result;
        }

        public function get updatedRecord():LogRecord
        {
            return operationRecord;
        }

        public function get deletedRecord():LogRecord
        {
            return operationRecord;
        }

        public function get record():LogRecord
        {
            return new LogRecord(lastData);
        }


        //---------------------------------------------------------------------
        //  Methods
        //---------------------------------------------------------------------

        /**
         * @constructor
         */
        public function LogService()
        {
            super();
        }

        /**
         * load records
         * @param uri
         */
        public function loadRecords():void
        {
            operationName = "loadRecords";
            super.load(Config.LOG_GET);
        }

        /**
         *
         * @param tweetString
         */
        public function tweet(tweetString:String):void
        {
            operationName = "addRecrod";
            super.load(Config.RECORD_ADD, URLRequestMethod.POST, {tweet: tweetString});
        }

        public function deleteRecord(record:LogRecord):void
        {
            operationName = "deleteRecord";
            operationRecord = record;
            super.load(Config.RECORD_DELETE, URLRequestMethod.POST, {id: record.id});
        }

        public function updateRecord(record:LogRecord):void
        {
            operationName = "updateRecord";
            operationRecord = record;
            var recordString:String = com.adobe.serialization.json.JSON.encode(record.toObject());
            super.load(Config.RECORD_UPDATE, URLRequestMethod.POST, {record: recordString});
        }


        //---------------------------------------------------------------------
        //
        //  Private
        //
        //---------------------------------------------------------------------

        private var operationName:String = "";
        private var operationRecord:LogRecord;

        private function faultHandler(event:IOErrorEvent):void
        {
            DebugWrapper.trace("ERROR: LogService" + operationName + ": " + event.text);
            operationName = "";
        }


    }
}
